import java.awt.Image;
import java.io.IOException;

import javax.imageio.ImageIO;


public class BossTarget extends Target {

	private static final int DAMAGE_TAKEN_BY_COLIBRIS = 1;
	private static final int DAMAGE_TAKEN_BY_MOINEAUX = 2;
	private static final int DAMAGE_TAKEN_BY_PIGEON   = 3;
	private static final int POINTS_GIVEN_WHEN_KILLED = 100;
	private static final int LIFE_POINTS = 3;

	private Image eyesOpenImage[] = new Image[3];
	private Image eyesClosedImage[] = new Image[3];

	private int cptSwapImage = 0;
	private int currentState = 0;

	public BossTarget(int posX, int posY) {
		this.loadRessources();
		this.init(posX,posY);
	}

	private void init(int posX,int posY) {
		super.setPosition(posX-this.imageWidth/3,posY-38);
		this.lifePoints = LIFE_POINTS;
		this.isLiving = true;
	}

	public void receiveDamage(int type) {
		int nb_damage = 0;
		if(type == Bird.COLIBRIS_TYPE)
			nb_damage = DAMAGE_TAKEN_BY_COLIBRIS;
		if(type == Bird.PIGEON_TYPE)
			nb_damage = DAMAGE_TAKEN_BY_MOINEAUX;
		if(type == Bird.MOINEAUX_TYPE)
			nb_damage = DAMAGE_TAKEN_BY_PIGEON;

		this.lifePoints -= nb_damage;
		if(this.lifePoints <= 0)
			this.setDead();
		else
			this.setImageBaseOnLifePointLeft();
	}

	private void setImageBaseOnLifePointLeft() {
		if(this.lifePoints < 2)      this.currentState = 2;
		else if(this.lifePoints < 3) this.currentState = 1;
		else                         this.currentState = 0;

		this.image = this.eyesOpenImage[this.currentState];
	}

	private void loadRessources() {
		try {
			this.loadExplosionImg();
			for(int i=0; i < 3 ; ++i){
				this.eyesOpenImage[i]   = ImageIO.read((getClass().getResource("Ressources/boss_open_"+i+".png")));
				this.eyesClosedImage[i] = ImageIO.read((getClass().getResource("Ressources/boss_closed_"+i+".png")));
			}
			this.image = eyesOpenImage[0];
			this.imageHeight = this.image.getHeight(null);
			this.imageWidth = this.image.getWidth(null);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	protected void update(Map map) {
		if(!this.isExploding()){
			this.eyeBlink();
		}
		else if(!this.hasExplode()){
			this.updateExplosion();
		}
	}

	private void eyeBlink() {
		if(Math.random() < 0.02) {
			this.image = this.eyesClosedImage[this.currentState];
		}
		else if(this.image == this.eyesClosedImage[this.currentState]) {
			if(this.cptSwapImage > 15) {
				this.cptSwapImage = 0;
				this.setImageBaseOnLifePointLeft();
			}
			else
				this.cptSwapImage++;
		}
	}

	@Override
	protected int getKillingPoints() {
		return POINTS_GIVEN_WHEN_KILLED;
	}

	@Override
	protected void setDead() {
		this.isLiving = false;
	}

	@Override
	protected void loadExplosionImg() {
		try {
			for(int i=0; i<this.imageArrayExplosion.length; ++i){
				this.imageArrayExplosion[i] = ImageIO.read((getClass().getResource("Ressources/explo_60_px_"+i+".png")));
			}
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
	}
}
